﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using DnPakExplorer.Controls;

namespace DnPakExplorer.Forms {
	public partial class SkillListForm : FormBase {

		SkillTableAdapter mAdapter;
		List<Skill> mSkills;
		Skill mSkill;
		InfoOverlayForm mInfoForm;
		Dntable mTable;

		public SkillListForm() {
			InitializeComponent();
			mTable = Dntable.UnionWith(DnResourceUtil.SkillTables);
			mSkills = new List<Skill>(mTable.RecordCount);
			for (int i=0; i<mTable.RecordCount; i++) {
				mSkills.Add(new Skill(i, mTable));
			}
			listGridView1.SetAdapter(mAdapter = new SkillTableAdapter(mSkills));
			listGridView1.Refresh();
		}


		private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
			if (e.KeyCode == Keys.Enter) {
				mAdapter.SetFilter(textBox1.Text, 0, false);
				listGridView1.NotifyDataChanged();
			}
		}

		private void textBox2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
			if (e.KeyCode == Keys.Enter) {
			}
		}

		private void listGridView1_MouseClick(object sender, MouseEventArgs e) {
			if (e.Button == System.Windows.Forms.MouseButtons.Left) {
				Point point = listGridView1.PointToCellIndex(e.Location);
				if (point.X>=-1 && point.Y>=0) {
					Skill = new Skill(mAdapter.GetRecordRow(point.Y), mTable);
				}
			}
		}

		public Skill Skill{
			get { return mSkill; }
			private set {
				if (mSkill == null || mSkill.SkillID != value.SkillID) {
					mSkill = value;
					iconCtrl1.IconId = mSkill.IconId;
					numericUpDown1.Minimum = 1;
					numericUpDown1.Maximum = mSkill.MaxLevel;
					numericUpDown1.Value = mSkill.Level;
					labelSkillName.Text = mSkill.Name;
					SetSkillLevel(mSkill.Level);
				}
			}
		}

		private void SetSkillLevel(int level) {
			if (Skill != null) {
				mSkill.Level = level;
				richText1.Text = mSkill.SkillDescrition.Replace("\\n", "\n");
			}
		}

		private void numericUpDown1_ValueChanged(object sender, EventArgs e) {
			if (Skill != null) {
				SetSkillLevel((int)numericUpDown1.Value);
			}
		}

		public class SkillTableAdapter : ListGridAdapter {

			List<Skill> mSkillList;
			int[] mFiltedItems;

			public SkillTableAdapter(List<Skill> skillList) {
				this.mSkillList = skillList;
			}

			public int GetRecordRow(int listRow) {
				if (mFiltedItems != null) {
					return mFiltedItems[listRow];
				}
				return listRow;
			}

			public void SetFilter(string filter, int column, bool regax) {
				if (String.IsNullOrEmpty(filter)) {
					mFiltedItems = null;
					return;
				}

				Grid g = new Grid();
				List<int> items = new List<int>();
				if (regax) {
					Regex pettern = new Regex(filter);
					if (column >= 0) {
						for (int r = 0; r < mSkillList.Count; r++) {
							GetGridDataNoFilted(r, column, g);
							if (g.text != null && pettern.IsMatch(g.text)) {
								items.Add(r);
							}
						}
					} else if (column == -1) {
						try {
							int value = int.Parse(filter);
							for (int r = 0; r < mSkillList.Count; r++) {
								if (pettern.IsMatch(mSkillList[r].SkillID.ToString())) {
									items.Add(r);
								}
							}
						} catch (FormatException) {
						}
					}
				} else {
					if (column >= 0) {
						for (int r = 0; r < mSkillList.Count; r++) {
							GetGridDataNoFilted(r, column, g);
							if (g.text != null && g.text.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0) {
								items.Add(r);
							}
						}
					} else if (column == -1) {
						try {
							int value = int.Parse(filter);
							for (int r = 0; r < mSkillList.Count; r++) {
								if (mSkillList[r].SkillID == value) {
									items.Add(r);
								}
							}
						} catch (FormatException) {
						}
					}
				}
				mFiltedItems = items.ToArray();
			}

			public override int RowCount {
				get {
					return mFiltedItems==null?mSkillList.Count:mFiltedItems.Length;
				}
			}

			public override int ColumnCount {
				get { return 4; }
			}

			public override void GetGridData(int row, int column, Grid grid) {
				if (mFiltedItems != null) {
					row = mFiltedItems[row];
				}
				GetGridDataNoFilted(row, column, grid);
			}

			private void GetGridDataNoFilted(int row, int column, Grid grid) {
				grid.textColor = Color.White;
				switch (column) {
				case 0:
					grid.text = mSkillList[row].Name;
					break;
				case 1:
					grid.text = Util.GetJobName(mSkillList[row].NeedJob);
					break;
				case 2:
					grid.text =  mSkillList[row].MaxLevel.ToString();
					break;
				case 3:
					grid.text =  mSkillList[row].SkillDescrition;
					break;
				}
				
			}

			public override void GetRowHeader(int row, Grid grid) {
				grid.textColor = Color.White;
				if (mFiltedItems != null) {
					row = mFiltedItems[row];
				}
				grid.text = mSkillList[row].SkillID.ToString();
			}

			public override void GetColumnHeader(int column, Grid grid) {
				grid.textColor = Color.White;
				switch (column) {
				case 0:
					grid.text = "技能名称";
					break;
				case 1:
					grid.text = "职业";
					break;
				case 2:
					grid.text = "最大等级";
					break;
				case 3:
					grid.text = "描述";
					break;
				}
			}

			public override void GetTopLeft(Grid grid) {
				grid.textColor = Color.White;
				grid.text = "_Key";
			}

			public void Clear() {
				mSkillList.Clear();
			}
		}

		private void iconCtrl1_MouseMove(object sender, MouseEventArgs e) {
			if (mSkill != null && iconCtrl1.ClientRectangle.Contains(e.Location)) {
				mInfoForm = InfoOverlayForm.GetOrCreateItemInfoForm();
				if (mInfoForm.Skill != mSkill) {
					mInfoForm.Skill = mSkill;
				}
				if (!mInfoForm.Visible) {
					mInfoForm.Show();
				}
				mInfoForm.UpdateInfoFormPosition();
				iconCtrl1.Capture = true;
			} else {
				if (mInfoForm != null) {
					mInfoForm.Hide();
					mInfoForm = null;
					iconCtrl1.Capture = false;
				}
			}
		}

		private void iconCtrl1_MouseLeave(object sender, EventArgs e) {
			if (mInfoForm != null) {
				mInfoForm.Hide();
				mInfoForm = null;
				iconCtrl1.Capture = false;
			}
		}

		private void button1_Click(object sender, EventArgs e) {
			if(mSkill != null)
				DnPlayerUtil.CurrentCharacter.SkillNanel.AddSkill(mSkill);
		}

		private void listGridView1_Load(object sender, EventArgs e) {

		}

		private void imageButton4_Click(object sender, EventArgs e) {
			Close();
		}


	}
}
